﻿//点击立即登录跳到登录页
enterloginpage.onclick = function(){
    registerpage.style.display = "none";
    loginpage.style.display = "block"
};
//点击立即注册跳到注册页
enterregisterpage.onclick = function(){
    loginpage.style.display = "none";
    registerpage.style.display = "block";
};
enterloginpage2.onclick = function(){
    forgetpasswordpage.style.display = "none";
    loginpage.style.display = "block"
};
enterresetpage.onclick = function(){
    loginpage.style.display = "none";
    forgetpasswordpage.style.display = "block"
};


//验证手机号
num1.onkeyup = function () {
    if(!num1.value){
        tishi1.style.visibility = "hidden";
    }
    if(num1.value.length==11){
        if(!y.test(num1.value)){
            tishi1.style.visibility = "visible";
            tishi1.innerHTML = "对不起，您的手机号格式有误";
        }else{
            tishi1.style.visibility = "hidden";
        }
    }
};







var y = /^1[345789]\d{9}$/;
var pwdReg0 = /^.{6,16}$/;
var pwdReg1 = /\d+/;
var pwdReg2 = /[a-zA-Z]+/;
var pwdReg3 = /[=~!@#\$%^&\*\(\)_\+\{\}\]:"\|',<?.\/\\]+/;
loginbtn.onclick = function () {
    if(!num1.value){
        tishi1.style.visibility = "visible";
        tishi1.innerHTML = "对不起，您的手机号不能为空";
        return;
    }
    if(!pwd.value){
        tishi2.style.visibility = "visible";
        tishi2.innerHTML = "对不起，您的密码不能为空";
        return;
    }
    if(!y.test(num1.value)){
        tishi1.style.visibility = "visible";
        tishi1.innerHTML = "对不起，您的手机号格式有误";
        return;
    }

    var x = pwd.value;
    var rightCount = 0;
    var lengthValid = pwdReg0.test(x);
    var numberValid = pwdReg1.test(x);
    var letterValid = pwdReg2.test(x);
    var symbolValid = pwdReg3.test(x);
    if(numberValid){
        rightCount++;
    }
    if(letterValid){
        rightCount++;
    }
    if(symbolValid){
        rightCount++;
    }
    if(!lengthValid||rightCount<2){
        tishi2.style.visibility = "visible";
        tishi2.innerHTML = "密码长度应为6~16位，数字、字母和符号至少包含两种";
    }else{
        $.post("http://test.hfjava.com/shop/userlogin",
            $("form").serialize(),
            function(data){
                if(data=="false"){
                    tishi2.style.visibility = "visible";
                    tishi2.innerHTML = "对不起，您的手机号或密码有误";
                }else{
                    alert("登录成功");
                }
            }
        );
    }
};




//注册页

var isPhoneRight = false;
var isYZMRight = false;
var isPsw1Right = false;
var isPsw2Right = false;


num3.onkeyup = function () {
    if(!num3.value){
        tishi3.style.visibility = "hidden";
        yanzhenbtn.setAttribute("disabled","disabled");
        isPhoneRight = false;
        return
    }
    if(num3.value.length==11){
        if(!y.test(num3.value)){
            tishi3.style.visibility = "visible";
            tishi3.innerHTML = "对不起，您的手机号格式有误";
            yanzhenbtn.setAttribute("disabled","disabled");
            isPhoneRight = false;
        }else{
            tishi3.style.visibility = "hidden";
            isPhoneRight = true;
            if(yanzhenbtn.value == "发送验证码"){
                yanzhenbtn.removeAttribute("disabled");
            }else{
                yanzhenbtn.setAttribute("disabled","disabled");
            }
        }
    }else{
        yanzhenbtn.setAttribute("disabled","disabled");
        isPhoneRight = false;
    }
};

num3.onblur = function () {
    if(!num3.value){
        tishi3.style.visibility = "visible";
        tishi3.innerHTML = "对不起，您的手机号不能为空";
        yanzhenbtn.setAttribute("disabled","disabled");
        isPhoneRight = false;
    }else{
        if(!y.test(num3.value)){
            tishi3.style.visibility = "visible";
            tishi3.innerHTML = "对不起，您的手机号格式有误";
            yanzhenbtn.setAttribute("disabled","disabled");
            isPhoneRight = false;
        }
    }
};

//发送验证码
var timer;
var t = 60;
var z = /^\d{6}/;
//倒计时开始
yanzhenbtn.onclick = function () {
    yanzhenbtn.setAttribute("disabled","disabled");
    yanzhenbtn.value = "重新发送60";
    timer = setInterval(daojishi,1000);
};
yanzhenbtn1.onclick = function () {
    yanzhenbtn1.setAttribute("disabled","disabled");
    yanzhenbtn1.value = "重新发送60";
    timer = setInterval(daojishi1,1000);
};
function daojishi1() {
    t--;
    if(t==0){
        clearInterval(timer);
        yanzhenbtn1.value = "发送验证码";
        yanzhenbtn1.removeAttribute("disabled");
        t = 60 ;
        return;
    }
    yanzhenbtn1.value = "重新发送"+t;
}
function daojishi() {
    t--;
    if(t==0){
        clearInterval(timer);
        yanzhenbtn.value = "发送验证码";
        yanzhenbtn.removeAttribute("disabled");
        t = 60 ;
        return;
    }
    yanzhenbtn.value = "重新发送"+t;
}
//倒计时结束

num4.onblur = function () {
    if (!z.test(num4.value)) {
        tishi4.style.visibility = "visible";
        tishi4.innerHTML = "对不起，您的验证码格式有误";
        isYZMRight = false
    }else{
        tishi4.style.visibility = "hidden";
        isYZMRight = true
    }
};
num9.onblur = function () {
    if (!z.test(num9.value)) {
        tishi9.style.visibility = "visible";
        tishi9.innerHTML = "对不起，您的验证码格式有误";
    }else{
        tishi9.style.visibility = "hidden";
    }
};

//验证码结束

//密码开始
pwd1.onblur = function () {
    var x = pwd1.value;
    var rightCount = 0;
    var lengthValid = pwdReg0.test(x);
    var numberValid = pwdReg1.test(x);
    var letterValid = pwdReg2.test(x);
    var symbolValid = pwdReg3.test(x);
    if(!x){
        tishi5.style.visibility = "visible";
        tishi5.innerHTML = "密码不能为空";
        isPsw1Right = false;
        return
    }
    if(numberValid){
        rightCount++;
    }
    if(letterValid){
        rightCount++;
    }
    if(symbolValid){
        rightCount++;
    }
    if(!lengthValid||rightCount<2){
        tishi5.style.visibility = "visible";
        tishi5.innerHTML = "密码长度应为6~16位，数字、字母和符号至少包含两种";
        isPsw1Right = false;
    }else{
        tishi5.style.visibility = "hidden";
        isPsw1Right = true;
    }
};


//
registerbtn.onclick = function () {
    var isRight = isPhoneRight&&isYZMRight&&isPsw1Right;
    if(isRight){
        var a = pwd1.value;
        var b = pwd2.value;
        if(!b){
            tishi6.style.visibility = "visible";
            tishi6.innerHTML = "确认密码不能为空";
        }else{
            if(a === b){
                tishi6.style.visibility = "hidden";
                location = "index.html"
            }else{
                tishi6.style.visibility = "visible";
                tishi6.innerHTML = "两次密码不一致";
            }
        }
    }
};

//重置密码页

num7.onkeyup = function () {
    if(!num7.value){
        tishi7.style.visibility = "hidden";
        yanzhenbtn1.setAttribute("disabled","disabled");
        return
    }
    if(num7.value.length==11){
        if(!y.test(num7.value)){
            tishi7.style.visibility = "visible";
            tishi7.innerHTML = "对不起，您的手机号格式有误";
            yanzhenbtn1.setAttribute("disabled","disabled");

        }else{
            tishi7.style.visibility = "hidden";
   
            if(yanzhenbtn1.value == "发送验证码"){
                yanzhenbtn1.removeAttribute("disabled");
            }else{
                yanzhenbtn1.setAttribute("disabled","disabled");
            }
        }
    }else{
        yanzhenbtn1.setAttribute("disabled","disabled");
    }
};

num7.onblur = function () {
    if(!num7.value){
        tishi7.style.visibility = "visible";
        tishi7.innerHTML = "对不起，您的手机号不能为空";
        yanzhenbtn1.setAttribute("disabled","disabled");

    }else{
        if(!y.test(num7.value)){
            tishi7.style.visibility = "visible";
            tishi7.innerHTML = "对不起，您的手机号格式有误";
            yanzhenbtn1.setAttribute("disabled","disabled");
        }
    }
};



resetbtn.onclick = function () {
    if(!num7.value){
        tishi7.style.visibility = "visible";
        tishi7.innerHTML = "对不起，您的手机号不能为空";
        return;
    }
    if(!pwd3.value){
        tishi8.style.visibility = "visible";
        tishi8.innerHTML = "对不起，您的密码不能为空";
        return;
    }
    if(!y.test(num7.value)){
        tishi7.style.visibility = "visible";
        tishi7.innerHTML = "对不起，您的手机号格式有误";
        return;
    }

    var x = pwd3.value;
    var rightCount = 0;
    var lengthValid = pwdReg0.test(x);
    var numberValid = pwdReg1.test(x);
    var letterValid = pwdReg2.test(x);
    var symbolValid = pwdReg3.test(x);
    if(numberValid){
        rightCount++;
    }
    if(letterValid){
        rightCount++;
    }
    if(symbolValid){
        rightCount++;
    }
    if(!lengthValid||rightCount<2){
        tishi8.style.visibility = "visible";
        tishi8.innerHTML = "密码长度应为6~16位，数字、字母和符号至少包含两种";
    }else{
        num7.value = "";
        pwd2.value = "";
        forgetpasswordpage.style.display = "none";
        loginpage.style.display = "block"
    }
};